/*     */ package com.slim.service.serviceimpl;
/*     */ 
/*     */ import java.util.List;

import org.appfuse.model.Deliverables;
import org.appfuse.model.NewProjectDetail;
import org.appfuse.model.User;
import org.appfuse.service.UserExistsException;
import org.appfuse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.slim.dao.DeliverablesDao;
import com.slim.dao.NewProjectDetailDao;
import com.slim.dao.UserDaoService;
import com.slim.service.UserManagerService;
/*     */ 
/*     */ @Service("userManagerImpl")
/*     */ public class UserManagerImpl extends GenericManagerImpl<User, Long>
/*     */   implements UserManagerService, UserService
/*     */ {
/*     */   private PasswordEncoder passwordEncoder;
/*     */   private UserDaoService userDaoServiceImpl;
			
			private NewProjectDetailDao newProjectDetailDaoImpl;
			private DeliverablesDao deliverablesDaoImpl;
/*     */ 
/*     */   @Autowired
/*     */   public void setPasswordEncoder(PasswordEncoder passwordEncoder)
/*     */   {
/*  32 */     this.passwordEncoder = passwordEncoder;
/*     */   }
/*     */   @Autowired
/*     */   public void setUserDaoServiceImpl(UserDaoService userDaoServiceImpl) {
/*  37 */     this.dao = userDaoServiceImpl;
/*  38 */     this.userDaoServiceImpl = userDaoServiceImpl;
/*     */   }
			@Autowired
			public void setNewProjectDetailDaoImpl(NewProjectDetailDao newProjectDetailDaoImpl) {
				this.newProjectDetailDaoImpl = newProjectDetailDaoImpl;
			}
						
/*     */ 
/*     */   public User getUser(String userId)
/*     */   {
/*  45 */     return (User)this.userDaoServiceImpl.get(new Long(userId));
/*     */   }
/*     */ 
/*     */   public List<User> getUsers()
/*     */   {
/*  52 */     return this.userDaoServiceImpl.getAllDistinct();
/*     */   }
/*     */ 
/*     */   public User saveUser(User user)
/*     */     throws UserExistsException
/*     */   {
/*  60 */     if (user.getVersion() == null)
/*     */     {
/*  62 */       user.setUsername(user.getUsername().toLowerCase());
/*     */     }
/*     */ 
/*  66 */     boolean passwordChanged = false;
/*  67 */     if (this.passwordEncoder != null)
/*     */     {
/*  69 */       if (user.getVersion() == null)
/*     */       {
/*  71 */         passwordChanged = true;
/*     */       }
/*     */       else {
/*  74 */         String currentPassword = this.userDaoServiceImpl.getUserPassword(user.getUsername());
/*  75 */         if (currentPassword == null) {
/*  76 */           passwordChanged = true;
/*     */         }
/*  78 */         else if (!currentPassword.equals(user.getPassword())) {
/*  79 */           passwordChanged = true;
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*  85 */       if (passwordChanged)
/*  86 */         user.setPassword(this.passwordEncoder.encodePassword(user.getPassword(), null));
/*     */     }
/*     */     else {
/*  89 */       this.log.warn("PasswordEncoder not set, skipping password encryption...");
/*     */     }
/*     */     try
/*     */     {
/*  93 */       return this.userDaoServiceImpl.saveUser(user);
/*     */     }
/*     */     catch (DataIntegrityViolationException e) {
/*  96 */       this.log.warn(e.getMessage());
/*  97 */       throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
/*     */     }
/*     */     catch (JpaSystemException e) {
/* 100 */       this.log.warn(e.getMessage());
/* 101 */     }throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
/*     */   }
/*     */ 
/*     */   public void removeUser(String userId)
/*     */   {
/* 109 */     this.log.debug("removing user: " + userId);
/* 110 */     this.userDaoServiceImpl.remove(new Long(userId));
/*     */   }
/*     */ 
/*     */   public User getUserByUsername(String username)
/*     */     throws UsernameNotFoundException
/*     */   {
/* 121 */     return (User)this.userDaoServiceImpl.loadUserByUsername(username);
/*     */   }
/*     */ 
/*     */   public List<User> search(String searchTerm)
/*     */   {
/* 128 */     return super.search(searchTerm, User.class);
/*     */   }
/*     */
		public NewProjectDetail saveNewProjectDetail(NewProjectDetail newProjectDetail) {
			return newProjectDetailDaoImpl.save(newProjectDetail);
		}
		/**
		 * @param newProjectDetailDaoImpl the newProjectDetailDaoImpl to set
		 */
		public Deliverables saveDeliverables(Deliverables deliverables) {
			return deliverablesDaoImpl.save(deliverables);
		}
}

